package com.isoftstone.industry.aaa.core.service.assist.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.isoftstone.industry.persistance.bean.TicketInfo;
import com.isoftstone.industry.aaa.core.dao.assist.TicketInfoDao;
import com.isoftstone.industry.aaa.core.service.assist.ITicketInfoService;
import com.isoftstone.modules.core.persistence.IGenericDao;
import com.isoftstone.modules.core.service.impl.GenericServiceImpl;
import com.isoftstone.modules.core.utils.DateUtils;

@Service
public class TicketInfoServiceImpl extends
		GenericServiceImpl<TicketInfo, String> implements ITicketInfoService {
	@Resource
	private TicketInfoDao ticketInfoDao;

	@Override
	public IGenericDao<TicketInfo, String> getDao() {
		return ticketInfoDao;
	}

	@Override
	public List<TicketInfo> findUnchargedTicketInfoByAppInstAndChargeMode(String appInstId, String chargeMode) {
		return ticketInfoDao.findByAppInstAndChargedAndChargeMode(appInstId, chargeMode, false);
	}

	@Override
	public void markCharged(String id) {
		ticketInfoDao.changeChargedProp(id, true, DateUtils.getCurrentTimestamp(DateUtils.YYYY_MM_DD_HH_MM_SS_PATTERN));
	}

	@Override
	public List<TicketInfo> findUnchargedTicketInfoByAppInstAndChargeModeAndCreatedTimeBetween(
			String appInstId, String chargeMode, Date start, Date end) {
		Timestamp _start = DateUtils.createStartTime(start);
		Timestamp _end = DateUtils.createEndTime(end);
		return ticketInfoDao.findByAppInstAndChargedAndChargeModeAndCreatedTimeBetween(appInstId, chargeMode, false, _start, _end);
	}

	@Override
	public void markCharged(String id, long chargeOrder) {
		ticketInfoDao.changeChargedAndChargeOrderProp(id, true, chargeOrder, DateUtils.getCurrentTimestamp(DateUtils.YYYY_MM_DD_HH_MM_SS_PATTERN));
	}

	@Override
	public long countCharged4All(String appInstId, String chargeMode) {
		return ticketInfoDao.countByCharged(appInstId, chargeMode, true);
	}

	@Override
	public long countCharged4Between(String appInstId, String chargeMode,
			Date start, Date end) {
		Timestamp _start = DateUtils.createStartTime(start);
		Timestamp _end = DateUtils.createEndTime(end);
		return ticketInfoDao.countByChargedAndCreatedTimeBetween(appInstId, chargeMode, true, _start, _end);
	}
	
	// 2015-04 产品包 start
	@Override
	public List<TicketInfo> findUnchargedTicketInfoByAppInstAndChargeMode(List<String> appInstIds, String chargeMode){
		return ticketInfoDao.findByAppInstsAndChargedAndChargeMode(appInstIds, chargeMode, false);
	}
	
	@Override
	public List<TicketInfo> findUnchargedTicketInfoByAppInstAndChargeModeAndCreatedTimeBetween(
			List<String> appInstIds, String chargeMode, Date start, Date end) {
		Timestamp _start = DateUtils.createStartTime(start);
		Timestamp _end = DateUtils.createEndTime(end);
		return ticketInfoDao.findByAppInstsAndChargedAndChargeModeAndCreatedTimeBetween(appInstIds, chargeMode, false, _start, _end);
	}
	
	@Override
	public long countCharged4All(List<String> appInstId, String chargeMode) {
		return ticketInfoDao.countByAppInstsAndCharged(appInstId, chargeMode, true);
	}

	@Override
	public long countCharged4Between(List<String> appInstId, String chargeMode,
			Date start, Date end) {
		Timestamp _start = DateUtils.createStartTime(start);
		Timestamp _end = DateUtils.createEndTime(end);
		return ticketInfoDao.countByAppInstsAndChargedAndCreatedTimeBetween(appInstId, chargeMode, true, _start, _end);
	}
	// 2015-04 产品包 end
}
